package com.jeasonzhao.commons.utils;

import java.util.Random;

public class RandomOrderList
{
    private RandomOrderList()
    {
    }
    public static final int[] pick(int totalamount,int pickcount)
    {
        return generate(0,totalamount,pickcount);
    }

    public static final int[] generate(int max)
    {
        return generate(0,max,-1);
    }
    public static final int[] generate(int min,int max)
    {
        return generate(min,max,-1);
    }

    private static final int[] generate(int nmin,int nmax,int length)
    {
        nmin=Math.abs(nmin);
        nmax=Math.abs(nmax);
        int min=Math.min(nmin,nmax);
        int max=Math.max(nmin,nmax);
        int maxlength=max-min;
        if(length>0)
        {
            maxlength=length;
        }
        java.util.ArrayList<Integer> ret=new java.util.ArrayList<Integer>();
        while(ret.size()<maxlength)
        {
            //rd.setSeed(System.currentTimeMillis()+rd.nextInt());
            int n=Math.abs( new Random(System.currentTimeMillis()+(new Random().nextInt())).nextInt(max));
            if(n<min||n>max||ret.contains(n))
            {
                //System.err.print(n+" ");
                continue;
            }
            else
            {
                ret.add(n);
            }
        }
        int[] retx=new int[ret.size()];
        for(int n=0;n<ret.size();n++)
        {
            retx[n]=ret.get(n);
        }
        return retx;
    }
    private static final void print(int[] nx)
    {
        for(int n: nx)
        {
            System.out.print(" "+Algorithms.toString(n,"0000"));
        }
        System.out.println();
    }
    public static void main(String[] argvs)
        throws InterruptedException
    {
        for(int n=0;n<1000;n++)
        {
            int[] x=pick(1000,5);
            if(x[0]<10)
            {
                System.out.print(">"+Algorithms.toString(n,"000000")+": ");
                print(x);
            }
            Thread.sleep(100);
        }
    }
}
